<?php
require_once('megamenu-admin.php');
define('CRT_MEGA_MENU', 'crt_mega_menu_');
add_action( 'admin_menu', 'add_support_scripts_backend',99999 );
function add_support_scripts_backend(){
	wp_enqueue_script('thickbox');
	wp_enqueue_style('thickbox');	
	wp_register_style( 'css_wd_menu_backend', THEME_CSS.'/megamenu_admin.css');
	wp_enqueue_style('css_wd_menu_backend');			
	
	wp_register_style( 'font-awesome', THEME_CSS.'/font-awesome.css');
	wp_enqueue_style('font-awesome');
	
	wp_register_script( 'bootstrap', THEME_JS.'/bootstrap.js',false,false,true);
	wp_enqueue_script('bootstrap');	
		
	wp_register_script( 'js_admin', THEME_JS.'/admin.js');
	wp_enqueue_script('js_admin');
}

/*
 * Show a sidebar select box
 */
function crt_list_sidebars($id){	
	global $wp_registered_sidebars;
	$output = '';
	$tempp = 'id="edit-menu-item-sidebars-'.$id.'" class="edit-menu-item-sidebars code" name="menu-item-sidebars['.$id.']"';
	$output .= "<select ".$tempp.">";
	$selection = get_post_meta( $id, CRT_MEGA_MENU.'menu_item_sidebars', true );
	
	foreach($wp_registered_sidebars as $sidebar_id => $sidebar) {
		$selected = $sidebar_id == $selection ? 'selected="selected"' : '';
		$output .= "<option ".$selected." value='".$sidebar_id."' >".$sidebar['name']."</option>";
	}
	$output .= "</select>";
	return $output;
}

add_action('wp_ajax_find_media_thumbnail', 'find_media_thumbnail');
function find_media_thumbnail(){
	$thumbnail_id = $_POST['thumbnail_id'];
	$img_arr =  wp_get_attachment_image_src( $thumbnail_id, 'full', true);
	echo $img_arr[0];
	die();
}

//update new field menu-item
add_action('wp_update_nav_menu_item', 'custom_nav_update',10, 3);
function custom_nav_update($menu_id, $menu_item_db_id, $menu_item_data ) {
	// menu status
    if ( is_array($_REQUEST['menu-item-megamenu-status']) ) {
        $custom_value = $_REQUEST['menu-item-megamenu-status'][$menu_item_db_id];
        update_post_meta( $menu_item_db_id, CRT_MEGA_MENU.'menu_item_megamenu_status', $custom_value );
    }
	
	//menu link disable
	if ( is_array($_REQUEST['menu-item-megamenu-link']) ) {
        $custom_value = $_REQUEST['menu-item-megamenu-link'][$menu_item_db_id];
        update_post_meta( $menu_item_db_id, CRT_MEGA_MENU.'menu_item_megamenu_link', $custom_value );
    }
	
	//menu description disable
	if ( is_array($_REQUEST['menu-item-megamenu-des']) ) {
        $custom_value = $_REQUEST['menu-item-megamenu-des'][$menu_item_db_id];
        update_post_meta( $menu_item_db_id, CRT_MEGA_MENU.'menu_item_megamenu_des', $custom_value );
    }
	
	//menu scroll to
	if ( is_array($_REQUEST['menu-item-scroll']) ) {
        $custom_value = $_REQUEST['menu-item-scroll'][$menu_item_db_id];
        update_post_meta( $menu_item_db_id, CRT_MEGA_MENU.'menu_item_scroll', $custom_value );
    }
	
	//menu icon
	if ( is_array($_REQUEST['menu-item-icon']) ) {
        $custom_value = $_REQUEST['menu-item-icon'][$menu_item_db_id];
        update_post_meta( $menu_item_db_id, CRT_MEGA_MENU.'menu_item_icon', $custom_value );
    }
	
	//menu background
	if ( is_array($_REQUEST['menu-item-background']) ) {
        $custom_value = $_REQUEST['menu-item-background'][$menu_item_db_id];
        update_post_meta( $menu_item_db_id, CRT_MEGA_MENU.'menu_item_background', $custom_value );
    }
	
	//menu background repeat
	if ( is_array($_REQUEST['menu-item-background-repeat']) ) {
        $custom_value = $_REQUEST['menu-item-background-repeat'][$menu_item_db_id];
        update_post_meta( $menu_item_db_id, CRT_MEGA_MENU.'menu_item_background_repeat', $custom_value );
    }
	
	//menu background position
	if ( is_array($_REQUEST['menu-item-background-position']) ) {
        $custom_value = $_REQUEST['menu-item-background-position'][$menu_item_db_id];
        update_post_meta( $menu_item_db_id, CRT_MEGA_MENU.'menu_item_background_position', $custom_value );
    }
	
	//menu columns
	if ( is_array($_REQUEST['menu-item-wide-column']) ) {
        $custom_value = $_REQUEST['menu-item-wide-column'][$menu_item_db_id];
        update_post_meta( $menu_item_db_id, CRT_MEGA_MENU.'menu_item_wide_column', $custom_value );
    }
	
	//menu columns
	if ( is_array($_REQUEST['menu-item-content-column']) ) {
        $custom_value = $_REQUEST['menu-item-content-column'][$menu_item_db_id];
        update_post_meta( $menu_item_db_id, CRT_MEGA_MENU.'menu_item_content_column', $custom_value );
    }
	
	//menu static html
	if ( is_array($_REQUEST['menu-item-static-html']) ) {
        $custom_value = base64_encode( htmlspecialchars( $_REQUEST['menu-item-static-html'][$menu_item_db_id] ));
        update_post_meta( $menu_item_db_id, CRT_MEGA_MENU.'menu_item_static_html', $custom_value );
    }
	
	//menu widget area
	if ( is_array($_REQUEST['menu-item-sidebars']) ) {
        $custom_value = $_REQUEST['menu-item-sidebars'][$menu_item_db_id];
        update_post_meta( $menu_item_db_id, CRT_MEGA_MENU.'menu_item_sidebars', $custom_value );
    }
}

/*
 * Adds value of new field to $item object that will be passed to Walker_Nav_Menu_Edit_Custom
 */
add_filter( 'wp_setup_nav_menu_item','custom_nav_item' );
function custom_nav_item($menu_item) {

	$menu_item->megamenu_status = get_post_meta( $menu_item->ID, CRT_MEGA_MENU.'menu_item_megamenu_status', true );
	$menu_item->megamenu_link = get_post_meta( $menu_item->ID, CRT_MEGA_MENU.'menu_item_megamenu_link', true );
	$menu_item->megamenu_des = get_post_meta( $menu_item->ID, CRT_MEGA_MENU.'menu_item_megamenu_des', true );
	$menu_item->scroll = get_post_meta( $menu_item->ID, CRT_MEGA_MENU.'menu_item_scroll', true );
    $menu_item->icon = get_post_meta( $menu_item->ID, CRT_MEGA_MENU.'menu_item_icon', true );
	$menu_item->background_id = get_post_meta( $menu_item->ID, CRT_MEGA_MENU.'menu_item_background', true );
	$menu_item->background_repeat = get_post_meta( $menu_item->ID, CRT_MEGA_MENU.'menu_item_background_repeat', true );
	$menu_item->background_position = get_post_meta( $menu_item->ID, CRT_MEGA_MENU.'menu_item_background_position', true );
	$menu_item->menu_column = get_post_meta( $menu_item->ID, CRT_MEGA_MENU.'menu_item_wide_column', true );
	$menu_item->menu_content_column = get_post_meta( $menu_item->ID, CRT_MEGA_MENU.'menu_item_content_column', true );
	$menu_item->static_html = get_post_meta( $menu_item->ID, CRT_MEGA_MENU.'menu_item_static_html', true );
	//$menu_item->sidebars = get_post_meta( $menu_item->ID, CRT_MEGA_MENU.'menu_item_sidebars', true );

    return $menu_item;
}

//Overide menu-item
add_filter( 'wp_edit_nav_menu_walker', 'crt_nav_edit_walker',10,2 );
function crt_nav_edit_walker($walker,$menu_id) {
    return 'Crt_Walker_Nav_Menu_Edit';
}

function has_sub($menu_item_id, &$items) {
	$sub_count = 0;
    foreach ($items as $item) {
	    if ($item->menu_item_parent && $item->menu_item_parent==$menu_item_id) {
		   $sub_count++;
		}
    }
    return $sub_count;
}
function modify_nav_items($items){
    foreach ($items as $item) {
        if ($sub_count = has_sub($item->ID, $items)) {
            $item->sub_count = $sub_count; 
        }
    }
    return $items;    
}

function crt_get_custom_sidebars($id){
}
function crt_get_custom_html($id){
	$html = '';
	
	$custom_content = get_post_meta( $id, CRT_MEGA_MENU.'menu_item_sidebars', true );	
	if( $custom_content ){
		$html.= '<div class="crt-content-html crt-custom-html">';
		$html.= do_shortcode( $custom_content );
		$html.= '</div>';
	}

	return $html;
}
add_filter('wp_nav_menu_objects', 'modify_nav_items');
?>